package com.atjyb.auth.controller;


import com.atjyb.auth.service.SysUserService;
import com.atjyb.common.result.Result;

import com.atjyb.common.utils.MD5;
import com.atjyb.model.system.SysUser;
import com.atjyb.vo.system.AssginRoleVo;
import com.atjyb.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atjyb
 * @since 2023-07-12
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService service;

    @ApiOperation("查询所有用户")
    @GetMapping("/getAllRoles")
    public Result getAllRoles() {
        return Result.ok(service.list());
    }

    /**
     * 条件分页查询
     * page 当前页
     * limit 分页数
     *
     * @return
     */
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable long page,
                                @PathVariable long limit,
                                SysUserQueryVo sysUserQueryVo
    ) {
        //1.page对象，封装分页查询的条件
        Page<SysUser> pageParam = new Page(page, limit);
        //2.封装条件查询的条件
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        if (!StringUtils.isEmpty(username)){
            //模糊查询
            lambdaQueryWrapper.like(SysUser::getUsername,username);
        }
        if (!StringUtils.isEmpty(createTimeBegin)){
            //模糊查询 ge  >=
            lambdaQueryWrapper.ge(SysUser::getCreateTime,createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeEnd)){
            //模糊查询 le  <=
            lambdaQueryWrapper.ge(SysUser::getCreateTime,createTimeEnd);
        }

        return Result.ok(service.page(pageParam,lambdaQueryWrapper));
    }

    @ApiOperation(value = "通过id获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser user = service.getById(id);
        return Result.ok(user);
    }

    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser user) {
        user.setPassword(MD5.encrypt(user.getPassword()));
        service.save(user);
        return Result.ok();
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser user) {
        service.updateById(user);
        return Result.ok();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        service.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        service.updateStatus(id, status);
        return Result.ok();
    }
}

